package com.eipc.net;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

public class HttpConnectionWraper {
	private final static int TIME_OUT = 30 * 1000;
	private String mUrl;
	private Context mContext;

	public HttpConnectionWraper(Context context,String url) {
		this.mUrl = url;
		mContext = context;
	}

	private boolean isWap() {
		String proxyHost = android.net.Proxy.getDefaultHost();
		if (proxyHost != null) {
			return true;
		} else {
			return false;
		}
	}

	public HttpURLConnection getConnection() {
		try {
			if (mUrl != null && !"".equals(mUrl)) {
				URL u = new URL(mUrl);
				HttpURLConnection connection = null;
				if (!isWap()) {
					connection = (HttpURLConnection) u.openConnection();
				} else {
					Proxy proxy = new Proxy(java.net.Proxy.Type.HTTP,
							new InetSocketAddress(android.net.Proxy.getHost(mContext), android.net.Proxy.getPort(mContext))); 
					connection = (HttpURLConnection) new URL(mUrl)
							.openConnection(proxy);
				}
				connection.setDoInput(true);
				connection.setDoInput(true);
				connection.setUseCaches(false);
				connection.setRequestMethod("POST");
				connection.setConnectTimeout(TIME_OUT);
				return connection;
			}
		} catch (Exception e) {
		}
		return null;
	}

	public String getContent() {
		InputStream is = null;
		HttpURLConnection connection = null;
		try {
			connection = getConnection();
			int responseCode = connection.getResponseCode();
			if (responseCode == 200) {
				is = connection.getInputStream();
				int len = is.available();
				if (len != -1) {
					byte[] result = new byte[len];
					is.read(result);
				return new String(result,"UTF-8");
				}
			}
			return null;
		} catch (Exception e) {
			Log.w("--------------------", e);
			return null;
		} finally {
			if (is != null)
				try {
					is.close();
				} catch (IOException e) {
				}
			if (connection != null)
				connection.disconnect();
		}
	}
	
	public Bitmap getPicture(){
		InputStream is = null;
		HttpURLConnection connection = null;
		Bitmap bitmap = null;
		try {
			connection = getConnection();
			int responseCode = connection.getResponseCode();
			if(responseCode==200){
				is = new BufferedInputStream(connection.getInputStream(),1024);
				bitmap = BitmapFactory.decodeStream(is);
				return bitmap;
			}
			return null;
		}
		 catch (Exception e) {
				return null;
			}
			finally{
				if(is!=null)
					try {
						is.close();
					} catch (IOException e) {
					}
				if(connection!=null) connection.disconnect();
			}
	}

	public byte[] getByte() {
		InputStream is = null;
		HttpURLConnection connection = null;
		try {
			connection = getConnection();
			int responseCode = connection.getResponseCode();
			if (responseCode == 200) {
				is = connection.getInputStream();
				int len = is.available();
				if (len != -1) {
					byte[] result = new byte[len];
					is.read(result);
					return result;
				}
			}
			return null;
		} catch (Exception e) {
			return null;
		} finally {
			if (is != null)
				try {
					is.close();
				} catch (IOException e) {
				}
			if (connection != null)
				connection.disconnect();
		}
	}
}
